package cn.xyinping.org.controller;

import cn.xyinping.basic.util.AjaxResult;
import cn.xyinping.basic.util.PageList;
import cn.xyinping.org.domain.Employee;
import cn.xyinping.org.query.EmployeeQuery;
import cn.xyinping.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;

/**
 * employee 员工操作
 * @author 27266
 * @desrciption
 * @date 2021/10/20
 */
@RestController // @RequestBody + @RequestMapping
@RequestMapping("/employee")
//@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询一行
     * @param id
     * @return
     */
     /*
        @GetMapping("/{id}") =  @RequestMapping(value =”/{id}”, method = RequesMethod.Get)
        @PathVariable("/id")
        以前是 localhost:8080/employee/qieryById?=2
        现在 localhost:8080/employee/qiery/2     restful风格
        @PathVariable 是接受前台传来的请求并传给id
     */
    @GetMapping("/{id}")
   // @ApiOperation(value = "查询一条数据",notes = "传一个id")
    public Employee queryById(@PathVariable("id") long id){
        return employeeService.findOne(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
   // @ApiOperation(value = "查询所有",notes = "不需要参数")
    public List<Employee> queryAll(){
        return employeeService.findAll();
    }

    /**
     * 分页查询
     * @RequestBody 前端传来的数据封装到对象中
     * @param query
     * @return
     */
    @PostMapping
   // @ApiOperation(value = "分页查询",notes = "分页。。。")
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryList(query);
    }

    /**
     * 删除一条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
   // @ApiOperation(value = "根据id删除",notes = "传一个id")
    public AjaxResult delete (@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me(); // 成功
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。"); // 失败
        }
    }
    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
   // @ApiOperation(value = "批量删除",notes = "传一个id集合")
    public AjaxResult patchDelete (@RequestBody List<Long> ids){
        try {
            employeeService.patchDelete(ids);
            return AjaxResult.me(); // 成功
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。"); // 失败
        }
    }

    /**
     * 添加和修改
     * @RequestBody 前端传来的数据封装到对象中
     * @param employee
     * @return
     */
    @PutMapping
    //@ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addAndUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){ // 判断id是否为空，空为添加，不空为修改
                employeeService.add(employee);
            }else {
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。。");
        }
    }

}
